package com.exam.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.MultiQuestion;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

//选择题
@Mapper
public interface MultiQuestionMapper {

    /** 查询指定考试中的组装过的选择题记录，封装到List集合中，代表一个选择题题库
     * 先在中间表paper_manager中查询当前试卷的选择题的questionId，在此questionId的范围内查找multi_question表中的记录，保证找到的选择题是经过试卷组装的。
     * @param PaperId
     * @return
     */
    @Select("select * from multi_question where questionId in (select questionId from paper_manage where questionType = 1 and paperId = #{paperId})")
    List<MultiQuestion> findByIdAndType(Integer PaperId);

    @Select("select * from multi_question")
    IPage<MultiQuestion> findAll(Page page);

    /** 查询最后一条记录的questionId
     * 将multi_question表根据questionId字段进行倒序，获取一条记录，即为刚刚新增记录的questionId（新增的在表中最后一条），返回此questionId给前端。
     * @return MultiQuestion
     */
    @Select("select questionId from multi_question order by questionId desc limit 1")
    MultiQuestion findOnlyQuestionId();

    @Options(useGeneratedKeys = true,keyProperty = "questionId")
    @Insert("insert into multi_question(subject,question,answerA,answerB,answerC,answerD,rightAnswer,analysis,section,level) " +
            "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{analysis},#{section},#{level})")
    int add(MultiQuestion multiQuestion);

    /** 随机查询指定条数的题目，返回questionId封装到集合中
     * 对multi_question表进行随机排序后，根据指定subject进行条件查询，查询指定数量的选择题，返回多条记录的questionId封装到List<Integer>集合中。
     * @param subject
     * @param pageNo
     * @return
     */
    @Select("select questionId from multi_question  where subject =#{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(String subject,Integer pageNo);


}
